package a66weding.com.jiehuntong.activity.home;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.xutils.x;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.fragment.category.bean.ShopDetBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import a66weding.com.jiehuntong.widgets.dialog.MyRedheartDialog;

/**
 * 作用：店铺详情中介绍店铺页面
 */
public class HomeShopdetailQRActivity extends BaseHomeActivity implements View.OnClickListener, OnNetResultListener {
    private LinearLayout mBacklay;
    private TextView mTitleTv;
    private ImageView mLabealIv;
    private TextView mCountTv;
    private TextView mCaseTv;
    private TextView mShopnewTv;
    private TextView mShopnameTv;
    private TextView mFansCountTv;
    private TextView mAddressTv;
    private TextView mContentTv;
    private TextView mShopTimeTv;
    private ImageView mAttentionIv;
    private int BusinessID;
    private boolean isLogin;
    private int memberID;
    private String nvc_num;
    private ShopDetBean.DataBean shopBean;
    private int NumberOffans;
    private ViewGroup mContentLay;
    private LinearLayout mNonetLay;
    private LinearLayout mProgressLay;
    private boolean isAttention;
    private LinearLayout mSearchLay;
    private final int  REQUEST_LOGIN_CODE=444;
    private  final int  DIALOG=1;
    private  Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                    case DIALOG:
                        if(myDialog!=null) {
                            myDialog.dismiss();
                        }
                    break;

            }
        }
    };
    private MyRedheartDialog myDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_shopdetail_qr);
        Intent intent = getIntent();
        if(intent!=null) {
            BusinessID =  intent.getIntExtra("BusinessID",0);
            nvc_num = intent.getStringExtra("nvc_num");
        }
        isLogin  = ((GlobalApplication)getApplication()).isLogin();
        if(isLogin) {
            user = DataWareHourse.getInstance().getmUser();
            memberID=user.getData().get(0).getI_Id();
        }
        initview();
        initdata();
    }
    private void initview() {
        mBacklay = (LinearLayout) findViewById(R.id.llay_header_back);
        mTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mContentLay = (ViewGroup) findViewById(R.id.scrview_content);
        mProgressLay = (LinearLayout) findViewById(R.id.lay_progress);
        mNonetLay = (LinearLayout) findViewById(R.id.lay_nonet);
        mLabealIv = (ImageView) findViewById(R.id.iv_lable_shop);
        mCountTv = (TextView) findViewById(R.id.shop_allp);
        mCaseTv = (TextView) findViewById(R.id.shop_case);
        mShopnewTv = (TextView) findViewById(R.id.shop_new);
        mShopnameTv = (TextView) findViewById(R.id.tv_shopdetail_name);
        mFansCountTv = (TextView) findViewById(R.id.tv_shopdetail_fans);
        mAddressTv = (TextView) findViewById(R.id.shop_content);
        mAttentionIv = (ImageView) findViewById(R.id.cate_attention);
        mContentTv = (TextView) findViewById(R.id.tv_shopdetail_content);
        mShopTimeTv = (TextView) findViewById(R.id.tv_shopdetail_time);
        mSearchLay = (LinearLayout) findViewById(R.id.lay_header_rightfunc);
        mTitleTv.setText("店铺详情");
        mAttentionIv.setClickable(false);
        mBacklay.setOnClickListener(this);
        mAttentionIv.setOnClickListener(this);
        mSearchLay.setOnClickListener(this);
        mSearchLay.setVisibility(View.GONE);
    }

    private void initdata() {
        getShopDetali();
    }
    /**
     * SJ16052616132669
     * GET WeddingMobileApi/get_BusinessDetail?Token={Token}&nvcNo={nvcNo}&memberID={memberID}
     * 返回商家详细信息
     * CollectionStare 是否被关注;
     */
    private  void getShopDetali(){
        String url = RequestCode.get_BusinessDetail+"&nvcNo="+nvc_num+"&memberID="+memberID;
        HttpRequestBean RequestBean = new HttpRequestBean(RequestCode.GETREQUEST,url,this, ShopDetBean.class,RequestCode.get_BusinessDetail);
        HttpHandleUtil.executeCommonGet(this,RequestBean);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                case R.id.llay_header_back:
                    finish();
                break;
                case R.id.cate_attention:
                    isAttention = shopBean.isCollectionStare();
                    if (isLogin){           // 如果已经登陆
                        attentionAndCancelFollow();
                    } else {        //没有登陆提示登陆
                        showLoginDialog();
                    }

                break;
            case R.id.lay_header_rightfunc:
                //进入下一个搜索界面;
                Intent intent = new Intent(this,ForSearchActivity.class);
                startActivity(intent);
                break;
        }
    }


    private void setData() {
        //添加数据;  getProductCount()
        mCountTv.setText(String.valueOf(shopBean.getProductCount()));
        mCaseTv.setText(String.valueOf(shopBean.getCasesCount()));
        mShopnewTv.setText(String.valueOf(shopBean.getNewCreate()));
        x.image().bind( mLabealIv,shopBean.getNvc_Logo(), ImageOptionsUtil.getOptions292_700_fixy());
        mShopnameTv.setText(shopBean.getNvc_StoreName());
        NumberOffans = shopBean.getBusinessMemberCount();
        mFansCountTv.setText(""+NumberOffans);
        mAddressTv.setText(shopBean.getNvc_Address());
        if (shopBean.isCollectionStare()){
            mAttentionIv.setImageResource(R.mipmap.img_home_already_attention);
        }else{
            mAttentionIv.setImageResource(R.mipmap.attentiontext);
        }
        mContentTv.setText(shopBean.getNvc_StoreInfo());
        String dt_addTime = shopBean.getDt_AddTime();
        dt_addTime= dt_addTime.substring(0,dt_addTime.indexOf("T"));
        mShopTimeTv.setText(dt_addTime);
    }
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof ShopDetBean) {
            refreshViewLay(HAVEDATA,mContentLay,mProgressLay,mNonetLay);
            ShopDetBean shopBeandata= (ShopDetBean) data;
            switch (shopBeandata.getResCode()) {
                    case "0000":
                        shopBean = shopBeandata.getData();
                        setData();
                        break;
            }
        }

        else if (data instanceof CommonResultBean){
            CommonResultBean commonData= (CommonResultBean) data;
            switch (commonData.getResCode()) {
                case "0000":
                    if(isAttention) {
                        mAttentionIv.setImageResource(R.mipmap.img_home_already_attention);
                        myDialog = new MyRedheartDialog(this,"关注成功!");
                        myDialog.show();
                        mHandler.sendEmptyMessageDelayed(DIALOG,800);
                        shopBean.setCollectionStare(true);
                    }else{
                        mAttentionIv.setImageResource(R.mipmap.attentiontext);
                        myDialog = new MyRedheartDialog(this,"成功取消关注!");
                        myDialog.show();
                        shopBean.setCollectionStare(false);
                        mHandler.sendEmptyMessageDelayed(DIALOG,800);
                    }
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        switch (errorBean.getErrorCode()){
            case ErrorBean.NONET:
            case ErrorBean.TIMEOUT:
                refreshViewLay(NONET,mContentLay,mProgressLay,mNonetLay);
                break;
            default:
                CUtils.showDataErrToast(this);
        }
    }

    public  void showLoginDialog() {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(this,   "亲，需要登录哦~", "去登录",  "取消", R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(HomeShopdetailQRActivity.this, LoginActivity.class);
                startActivityForResult(intent,REQUEST_LOGIN_CODE);
                confirmDialog.dismiss();
            }
            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==LoginActivity.LOGIN_OK&&requestCode==REQUEST_LOGIN_CODE){
            attentionAndCancelFollow();
        }
    }

    /**
     * 关注与取消关注
     */
    public void attentionAndCancelFollow(){
        if(isAttention) {
            String cancleUrl= RequestCode.post_CancelBusinessMember;
            cancleUrl += "&MemberID="+ memberID + "&BusinessID=" + BusinessID;
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, cancleUrl,
                    this, CommonResultBean.class, RequestCode.post_CancelBusinessMember);
            HttpHandleUtil.executeCommonPost(this,httpRequestBean);
            isAttention =false;

        }else{//关注
            String url=RequestCode.post_BusinessMemberCreate;
            url += "&MemberID="+memberID+ "&BusinessID=" + BusinessID;
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                    this, CommonResultBean.class, RequestCode.POST_BUSINESSMEMBERCREATE);
            HttpHandleUtil.executeCommonPost(this,httpRequestBean);
            isAttention =true;
        }
    }

}
